package com.pro01.backend.controller;

import com.pro01.backend.pojo.Forum;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.ForumService;
import com.pro01.backend.utils.JwtUtil;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理员管理论坛接口
 */
@RestController
@RequestMapping("/admin/forums")
public class AdminForumController {

    @Autowired
    private ForumService forumService;

//

    /**
     * 判断当前用户是否为管理员
     */
    private boolean isAdmin(HttpServletRequest request) {
        try {
            String role = TokenUtil.getRole(request); // ✅ 直接从工具类取角色
            return "ADMIN".equals(role);
        } catch (Exception e) {
            return false; // token 无效或缺失
        }
    }


    /**
     * 创建板块(管理员)
     */
    @PostMapping
    public Result<Void> createForum(@RequestBody Forum forum, HttpServletRequest request) {

        if (!isAdmin(request)) {
            return Result.error("无权限，只有管理员可以创建板块！");
        }

        try {
            forumService.createForum(forum);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建板块失败：" + e.getMessage());
        }
    }

    /**
     * 查询所有板块列表(管理员)
     */
    @GetMapping
    public Result<List<Forum>> getAllForums(HttpServletRequest request) {
        if (!isAdmin(request)) {
            return Result.error("无权限，只有管理员可以查看板块！");
        }

        try {
            List<Forum> forums = forumService.getAllForums();
            return Result.success(forums);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询板块失败：" + e.getMessage());
        }
    }

    /**
     * 删除板块(管理员)
     */
    @DeleteMapping("/{forumId}")
    public Result<Void> deleteForum(@PathVariable Long forumId, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return Result.error("无权限，只有管理员可以删除板块！");
        }

        try {
            forumService.deleteForum(forumId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除板块失败：" + e.getMessage());
        }
    }

    /**
     * 管理员编辑板块信息
     */
    @PutMapping("/{forumId}")
    public Result<Void> updateForum(@PathVariable Long forumId,
                                    @RequestBody Forum forum,
                                    HttpServletRequest request) {
        if (!isAdmin(request)) {
            return Result.error("无权限，只有管理员可以编辑板块！");
        }

        try {
            forum.setId(forumId); // 确保 ID 设置正确
            forumService.updateForum(forum);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("编辑板块失败：" + e.getMessage());
        }
    }
}
